#include <STC89C5xRC.H>
#include "LCD1602.h"
#include "ds1302.h"
#include "delay.h"
#include "key.h"
#include "timer0.h"


unsigned char keynum,mode,timesetselect,timesetflashflag;
unsigned char second,minute;

void timeshow()
{
	ds1302_readtime();
	LCD_ShowNum(1,1,ds1302_time[0],2);
	LCD_ShowNum(1,4,ds1302_time[1],2);
	LCD_ShowNum(1,7,ds1302_time[2],2);
	LCD_ShowNum(1,14,ds1302_time[6],2);
	LCD_ShowNum(2,1,ds1302_time[3],2);
	LCD_ShowNum(2,4,ds1302_time[4],2);
	LCD_ShowNum(2,7,ds1302_time[5],2);
}

void timeset()
{
	if(keynum == 2)
	{
		timesetselect++;
		if(timesetselect>6){timesetselect=0;}
	}
	if(keynum == 3)
	{
		ds1302_time[timesetselect]++;
		if(ds1302_time[0]>99){ds1302_time[0]=0;}
		if(ds1302_time[1]>12){ds1302_time[1]=1;}
		if(ds1302_time[1]==1||ds1302_time[1]==3||ds1302_time[1]==5||ds1302_time[1]==7||ds1302_time[1]==8||ds1302_time[1]==12)
		{
			if(ds1302_time[2]>31){ds1302_time[2]=1;}
		}
		else if(ds1302_time[1]==4||ds1302_time[1]==6||ds1302_time[1]==9||ds1302_time[1]==10||ds1302_time[1]==11)
		{
			if(ds1302_time[2]>30){ds1302_time[2]=1;}
		}
		else if(ds1302_time[1]==2)
		{
			if(((ds1302_time[0]%4 ==0) &&(ds1302_time[0]%100 !=0)) || ds1302_time[0]%400 == 0)
			{
				if(ds1302_time[2]>28){ds1302_time[2]=1;}
			}
			else
			{
			if(ds1302_time[2]>29){ds1302_time[2]=1;}
			}
		}

		if(ds1302_time[3]>24){ds1302_time[3]=0;}
		if(ds1302_time[4]>60){ds1302_time[4]=0;}
		if(ds1302_time[5]>60){ds1302_time[5]=0;}
		if(ds1302_time[6]>7){ds1302_time[6]=1;}
	}
	if(keynum==4)
	{
		ds1302_time[timesetselect]--;
		if(ds1302_time[0]<0){ds1302_time[0]=99;}
		if(ds1302_time[1]<1){ds1302_time[1]=12;}
		if(ds1302_time[1]==1||ds1302_time[1]==3||ds1302_time[1]==5||ds1302_time[1]==7||ds1302_time[1]==8||ds1302_time[1]==12)
		{
			if(ds1302_time[2]<1){ds1302_time[2]=31;}
			if(ds1302_time[2]>31){ds1302_time[2]=31;}
		}
		else if(ds1302_time[1]==4||ds1302_time[1]==6||ds1302_time[1]==9||ds1302_time[1]==10||ds1302_time[1]==11)
		{
			if(ds1302_time[2]<1){ds1302_time[2]=30;}
			if(ds1302_time[2]>30){ds1302_time[2]=30;}
		}
		else if(ds1302_time[1]==2)
		{
			if(((ds1302_time[0]%4 ==0) &&(ds1302_time[0]%100 !=0)) || ds1302_time[0]%400 == 0)
			{
				if(ds1302_time[2]<1){ds1302_time[2]=28;}
				if(ds1302_time[2]>28){ds1302_time[2]=28;}
			}
			else
			{
			if(ds1302_time[2]<1){ds1302_time[2]=29;}
			if(ds1302_time[2]>29){ds1302_time[2]=29;}
			}
		}

		if(ds1302_time[3]>24){ds1302_time[3]=24;}
		if(ds1302_time[4]>60){ds1302_time[4]=60;}
		if(ds1302_time[5]>60){ds1302_time[5]=60;}
		if(ds1302_time[6]<1){ds1302_time[6]=7;}
	}
	if(timesetselect==0 && timesetflashflag==1){LCD_ShowString(1,1,"  ");}
	else {LCD_ShowNum(1,1,ds1302_time[0],2);}
	if(timesetselect==1 && timesetflashflag==1){LCD_ShowString(1,4,"  ");}
	else {LCD_ShowNum(1,4,ds1302_time[1],2);}
	if(timesetselect==2 && timesetflashflag==1){LCD_ShowString(1,7,"  ");}
	else {LCD_ShowNum(1,7,ds1302_time[2],2);}
	if(timesetselect==6 && timesetflashflag==1){LCD_ShowString(1,14,"  ");}
	else {LCD_ShowNum(1,14,ds1302_time[6],2);}
	if(timesetselect==3 && timesetflashflag==1){LCD_ShowString(2,1,"  ");}
	else {LCD_ShowNum(2,1,ds1302_time[3],2);}
	if(timesetselect==4 && timesetflashflag==1){LCD_ShowString(2,4,"  ");}
	else {LCD_ShowNum(2,4,ds1302_time[4],2);}
	if(timesetselect==5 && timesetflashflag==1){LCD_ShowString(2,7,"  ");}
	else {LCD_ShowNum(2,7,ds1302_time[5],2);}

	LCD_ShowNum(2,10,timesetselect,2);
	LCD_ShowNum(2,13,timesetflashflag,2);


}

void main()
{
	time0_init();
	ds1302_init();
	//ds1302_writebyte(0x8e,0x00);
	LCD_Init();	
	LCD_ShowString(1,1,"  /  /   Day:");
	LCD_ShowString(2,1,"  :  :");
	
	ds1302_settime();
		
	while(1)
	{
		keynum = key();
		if(keynum==1)
		{
			if(mode==0){mode =1;timesetselect=0;}
			else if(mode==1){mode=0;ds1302_settime();}

		}
		switch(mode)
		{
			case 0:timeshow();break;
			case 1:timeset();break;
				
			
			
		}
	}

}

//定时器中断函数模板
void timer0_routine() interrupt 1
{
	static unsigned int t0count;
	TH0=64535/256;
	TL0=64535%256;
	t0count++;
	if(t0count>=500)
	{
		t0count = 0;
		timesetflashflag=!timesetflashflag;
	}
}
